#opencv/platforms/linux
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR arm)

set(GCC_COMPILER_VERSION "4.8" CACHE STRING "GCC Compiler version")
set(FLOAT_ABI_SUFFIX "")

if (NOT SOFTFP)
  set(FLOAT_ABI_SUFFIX "hf")
endif()

if(USE_NEON)
  message(WARNING "You use obsolete variable USE_NEON to enable NEON instruction set. Use -DENABLE_NEON=ON instead." )
  set(ENABLE_NEON TRUE)
elseif(USE_VFPV3)
  message(WARNING "You use obsolete variable USE_VFPV3 to enable VFPV3 instruction set. Use -DENABLE_VFPV3=ON instead." )
  set(ENABLE_VFPV3 TRUE)
endif()

#set(ARM_ABI "armeabi-v7a with NEON")
set( ARM_ABI $ENV{ARM_ABI} CACHE STRING "The target ABI for Android. If arm, then armeabi-v7a is recommended for hardware floating point." FORCE )

message("ARM_ABI=${ARM_ABI}")
# set target ABI options
if( ARM_ABI STREQUAL "arm64-v8a" )
 set( ARM64_V8A true )
 set( ARM_NDK_ABI_NAME "arm64-v8a" )
 set( ARM_ARCH_NAME "arm64" )
 set( ARM_LLVM_TRIPLE "aarch64-none-linux-android" )
 set( CMAKE_SYSTEM_PROCESSOR "aarch64" )
 set( ENABLE_VFPV3 true )
 set( ENABLE_NEON true )
 set(FLOAT_ABI_SUFFIX "")
elseif( ARM_ABI STREQUAL "armeabi" )
 set( ARMEABI true )
 set( ARM_NDK_ABI_NAME "armeabi" )
 set( ARM_ARCH_NAME "arm" )
 set( ARM_LLVM_TRIPLE "armv5te-none-linux-androideabi" )
 set( CMAKE_SYSTEM_PROCESSOR "armv5te" )
elseif( ARM_ABI STREQUAL "armeabi-v6 with VFP" )
 set( ARMEABI_V6 true )
 set( ARM_NDK_ABI_NAME "armeabi" )
 set( ARM_ARCH_NAME "arm" )
 set( ARM_LLVM_TRIPLE "armv5te-none-linux-androideabi" )
 set( CMAKE_SYSTEM_PROCESSOR "armv6" )
 # need always fallback to older platform
 set( ARMEABI true )
elseif( ARM_ABI STREQUAL "armeabi-v7a")
 set( ARMEABI_V7A true )
 set( ARM_NDK_ABI_NAME "armeabi-v7a" )
 set( ARM_ARCH_NAME "arm" )
 set( ARM_LLVM_TRIPLE "armv7-none-linux-androideabi" )
 set( CMAKE_SYSTEM_PROCESSOR "armv7-a" )
elseif( ARM_ABI STREQUAL "armeabi-v7a with VFPV3" )
 set( ARMEABI_V7A true )
 set( ARM_NDK_ABI_NAME "armeabi-v7a" )
 set( ARM_ARCH_NAME "arm" )
 set( ARM_LLVM_TRIPLE "armv7-none-linux-androideabi" )
 set( CMAKE_SYSTEM_PROCESSOR "armv7-a" )
 set( ENABLE_VFPV3 true )
elseif( ARM_ABI STREQUAL "armeabi-v7a with NEON" )
 set( ARMEABI_V7A true )
 set( ARM_NDK_ABI_NAME "armeabi-v7a" )
 set( ARM_ARCH_NAME "arm" )
 set( ARM_LLVM_TRIPLE "armv7-none-linux-androideabi" )
 set( CMAKE_SYSTEM_PROCESSOR "armv7-a" )
 set( ENABLE_VFPV3 true )
 set( ENABLE_NEON true )
else()
 message( "Unknown ARM_ABI=${ARM_ABI} is specified." )
endif()
message("ARM_ABI=${ARM_ABI}")

# ABI-specific flags
set(CMAKE_C_COMPILER    /usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-gcc-${GCC_COMPILER_VERSION})
set(CMAKE_CXX_COMPILER  /usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-g++-${GCC_COMPILER_VERSION})
set(ARM_LINUX_SYSROOT /usr/arm-linux-gnueabi${FLOAT_ABI_SUFFIX} CACHE PATH "ARM cross compilation system root")

#fortran
#set(CMAKE_Fortran_COMPILER    /usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-gfortran-${GCC_COMPILER_VERSION})

set(CMAKE_CXX_FLAGS           ""                    CACHE STRING "c++ flags")
set(CMAKE_C_FLAGS             ""                    CACHE STRING "c flags")
set(CMAKE_SHARED_LINKER_FLAGS ""                    CACHE STRING "shared linker flags")
set(CMAKE_MODULE_LINKER_FLAGS ""                    CACHE STRING "module linker flags")
set(CMAKE_EXE_LINKER_FLAGS    "-Wl,-z,nocopyreloc"  CACHE STRING "executable linker flags")

unset( ARM_CXX_FLAGS )
if( ARM64_V8A )
 set( ARM_CXX_FLAGS "${ARM_CXX_FLAGS} -march=armv8-a+fp+simd -ftree-vectorize -mtune=cortex-a57 -mcpu=cortex-a57" )
 set(CMAKE_C_COMPILER    /usr/bin/aarch64-linux-gnu-gcc-${GCC_COMPILER_VERSION})
 set(CMAKE_CXX_COMPILER  /usr/bin/aarch64-linux-gnu-g++-${GCC_COMPILER_VERSION})
 set(ARM_LINUX_SYSROOT /usr/aarch64-linux-gnu CACHE PATH "ARM cross compilation system root")

 #fortran
 #set(CMAKE_Fortran_COMPILER    /usr/bin/aarch64-linux-gnu-gfortran-${GCC_COMPILER_VERSION})
# message ("*** arm64-v8a Fortran compiler: " ${CMAKE_Fortran_COMPILER})

 set( CMAKE_ASM_COMPILER "/usr/bin/aarch64-linux-gnu-as"     CACHE PATH "assembler" )
 set( CMAKE_STRIP        "/usr/bin/aarch64-linux-gnu-strip"   CACHE PATH "strip" )
 set( CMAKE_AR           "/usr/bin/aarch64-linux-gnu-ar"      CACHE PATH "archive" )
# set( CMAKE_LINKER       "/usr/bin/aarch64-linux-gnu-ld"      CACHE PATH "linker" )
 set( CMAKE_LINKER       "/usr/bin/aarch64-linux-gnu-gcc"      CACHE PATH "linker" )
 set( CMAKE_NM           "/usr/bin/aarch64-linux-gnu-nm"      CACHE PATH "nm" )
 set( CMAKE_OBJCOPY      "/usr/bin/aarch64-linux-gnu-objcopy" CACHE PATH "objcopy" )
 set( CMAKE_OBJDUMP      "/usr/bin/aarch64-linux-gnu-objdump" CACHE PATH "objdump" )
 set( CMAKE_RANLIB       "/usr/bin/aarch64-linux-gnu-ranlib"  CACHE PATH "ranlib" )

 set(CMAKE_SHARED_LINKER_FLAGS "-rdynamic -Wl,--no-undefined -Wl,--gc-sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now ${CMAKE_SHARED_LINKER_FLAGS}")
 set(CMAKE_MODULE_LINKER_FLAGS "-rdynamic -Wl,--no-undefined -Wl,--gc-sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now ${CMAKE_MODULE_LINKER_FLAGS}")
 set(CMAKE_EXE_LINKER_FLAGS    "-rdynamic -Wl,--no-undefined -Wl,--gc-sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now ${CMAKE_EXE_LINKER_FLAGS}")

elseif( ARMEABI_V7A )
 set( ARM_CXX_FLAGS "${ARM_CXX_FLAGS} -marm -mthumb -march=armv7-a -mfloat-abi=hard" )
 #cortex-a8
 set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,--gc-sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now ${CMAKE_SHARED_LINKER_FLAGS}")
 set(CMAKE_MODULE_LINKER_FLAGS "-Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,--gc-sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now ${CMAKE_MODULE_LINKER_FLAGS}")
 set(CMAKE_EXE_LINKER_FLAGS    "-Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,--gc-sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now ${CMAKE_EXE_LINKER_FLAGS}")
#default
 set( CMAKE_ASM_COMPILER "/usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-as"     CACHE PATH "assembler" )
 set( CMAKE_STRIP        "/usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-strip"   CACHE PATH "strip" )
 set( CMAKE_AR           "/usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-ar"      CACHE PATH "archive" )
 set( CMAKE_LINKER       "/usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-ld"      CACHE PATH "linker" )
 set( CMAKE_NM           "/usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-nm"      CACHE PATH "nm" )
 set( CMAKE_OBJCOPY      "/usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-objcopy" CACHE PATH "objcopy" )
 set( CMAKE_OBJDUMP      "/usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-objdump" CACHE PATH "objdump" )
 set( CMAKE_RANLIB       "/usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-ranlib"  CACHE PATH "ranlib" )

 if( ENABLE_NEON )
  set( ARM_CXX_FLAGS "${ARM_CXX_FLAGS} -mfpu=neon" )
 elseif( ENABLE_VFPV3 )
  set( ARM_CXX_FLAGS "${ARM_CXX_FLAGS} -mfpu=vfpv3" )
 else()
  set( ARM_CXX_FLAGS "${ARM_CXX_FLAGS} -mfpu=vfpv3-d16" )
 endif()
elseif( ARMEABI_V6 )
 set( ARM_CXX_FLAGS "${ARM_CXX_FLAGS} -marm -mthumb -march=armv6 -mfloat-abi=hard -mfpu=vfp" ) # vfp == vfpv2
 #cortex-a8
 set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,--gc-sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now ${CMAKE_SHARED_LINKER_FLAGS}")
 set(CMAKE_MODULE_LINKER_FLAGS "-Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,--gc-sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now ${CMAKE_MODULE_LINKER_FLAGS}")
 set(CMAKE_EXE_LINKER_FLAGS    "-Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,--gc-sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now ${CMAKE_EXE_LINKER_FLAGS}")
 set( CMAKE_ASM_COMPILER "/usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-as"     CACHE PATH "assembler" )
 set( CMAKE_STRIP        "/usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-strip"   CACHE PATH "strip" )
 set( CMAKE_AR           "/usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-ar"      CACHE PATH "archive" )
 set( CMAKE_LINKER       "/usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-ld"      CACHE PATH "linker" )
 set( CMAKE_NM           "/usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-nm"      CACHE PATH "nm" )
 set( CMAKE_OBJCOPY      "/usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-objcopy" CACHE PATH "objcopy" )
 set( CMAKE_OBJDUMP      "/usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-objdump" CACHE PATH "objdump" )
 set( CMAKE_RANLIB       "/usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-ranlib"  CACHE PATH "ranlib" )
elseif( ARMEABI )
# set( ARM_CXX_FLAGS "${ARM_CXX_FLAGS} -march=armv5te -mtune=xscale -mfloat-abi=hard" )
 set( ARM_CXX_FLAGS "${ARM_CXX_FLAGS} -marm -mthumb -mfpu=vfp -mfloat-abi=hard -mtune=xscale" )
 #cortex-a8
 set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,--gc-sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now ${CMAKE_SHARED_LINKER_FLAGS}")
 set(CMAKE_MODULE_LINKER_FLAGS "-Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,--gc-sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now ${CMAKE_MODULE_LINKER_FLAGS}")
 set(CMAKE_EXE_LINKER_FLAGS    "-Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,--gc-sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now ${CMAKE_EXE_LINKER_FLAGS}")
 set( CMAKE_ASM_COMPILER "/usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-as"     CACHE PATH "assembler" )
 set( CMAKE_STRIP        "/usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-strip"   CACHE PATH "strip" )
 set( CMAKE_AR           "/usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-ar"      CACHE PATH "archive" )
 set( CMAKE_LINKER       "/usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-ld"      CACHE PATH "linker" )
 set( CMAKE_NM           "/usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-nm"      CACHE PATH "nm" )
 set( CMAKE_OBJCOPY      "/usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-objcopy" CACHE PATH "objcopy" )
 set( CMAKE_OBJDUMP      "/usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-objdump" CACHE PATH "objdump" )
 set( CMAKE_RANLIB       "/usr/bin/arm-linux-gnueabi${FLOAT_ABI_SUFFIX}-ranlib"  CACHE PATH "ranlib" )
endif()

message("ENABLE_NEON=${ENABLE_NEON}")
message("ARM_CXX_FLAGS=${ARM_CXX_FLAGS}")
set( CMAKE_CXX_FLAGS           "${ARM_CXX_FLAGS} ${CMAKE_CXX_FLAGS}" )
set( CMAKE_C_FLAGS             "${ARM_CXX_FLAGS} ${CMAKE_C_FLAGS}" )

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -fdata-sections -Wa,--noexecstack -fsigned-char -Wno-psabi")
set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -pthread -fdata-sections -Wa,--noexecstack -fsigned-char -Wno-psabi")
	
set(CMAKE_FIND_ROOT_PATH ${CMAKE_FIND_ROOT_PATH} ${ARM_LINUX_SYSROOT})

if(EXISTS ${CUDA_TOOLKIT_ROOT_DIR})
    set(CMAKE_FIND_ROOT_PATH ${CMAKE_FIND_ROOT_PATH} ${CUDA_TOOLKIT_ROOT_DIR})
endif()

add_definitions(-D__ARM_ABI__="${ARM_ABI}")

set( CMAKE_SKIP_RPATH TRUE CACHE BOOL "If set, runtime paths are not added when using shared libraries." )
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ONLY)

# macro to find programs on the host OS
macro( find_host_program )
 set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER )
 set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY NEVER )
 set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE NEVER )
 if( CMAKE_HOST_WIN32 )
  SET( WIN32 1 )
  SET( UNIX )
 elseif( CMAKE_HOST_APPLE )
  SET( APPLE 1 )
  SET( UNIX )
 endif()
 find_program( ${ARGN} )
 SET( WIN32 )
 SET( APPLE )
 SET( UNIX 1 )
 set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ONLY )
 set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY )
 set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY )
endmacro()

# macro to find packages on the host OS
macro( find_host_package )
 set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER )
 set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY NEVER )
 set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE NEVER )
 if( CMAKE_HOST_WIN32 )
  SET( WIN32 1 )
  SET( UNIX )
 elseif( CMAKE_HOST_APPLE )
  SET( APPLE 1 )
  SET( UNIX )
 endif()
 find_package( ${ARGN} )
 SET( WIN32 )
 SET( APPLE )
 SET( UNIX 1 )
 set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ONLY )
 set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY )
 set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY )
endmacro()
